package whf.framework.datasource.impl;

import java.io.Serializable;
import java.util.Map;

import org.jdom.Element;

import whf.framework.datasource.DataRow;
import whf.framework.exception.PropertyException;
import whf.framework.util.BeanUtils;

/**
 * @author king
 * @modify 2008-4-12 下午03:01:43
 * 
 */
@SuppressWarnings("unchecked")
public class MapDataRow implements DataRow {
	private Map map;
	private String primaryKeyField;
	private String displayNameField;

	public MapDataRow(Map map, String primaryKeyField, String displayNameField) {
		assert map == null: "data map can not be null";
		assert primaryKeyField == null: "Primary key field can not be null!";
		assert displayNameField == null: "Display name field can not be null";
		this.map = map;
		this.primaryKeyField = primaryKeyField;
		this.displayNameField = displayNameField;
	}

	public String getDisplayName() {
		Object value = this.map.get(this.displayNameField);
		return value == null? "": value.toString();
	}

	public Serializable getPrimaryKey() {
		Object value = this.map.get(this.primaryKeyField);
		return (Serializable)value;
	}

	public Object getProperty(String propertyName) throws PropertyException {
		return BeanUtils.getProperty(map, propertyName);
	}

	public String getPropertyDisplay(String propertyName) throws PropertyException {
		Object value = this.getProperty(propertyName);
		return value == null? "": value.toString();
	}

	public void setProperty(String propertyName, Class propertyType, String value) throws PropertyException {
		BeanUtils.setProperty(map, propertyType, propertyName, value);
	}

	public void setProperty(String propertyName, Object value) throws PropertyException {
		BeanUtils.setProperty(map, propertyName, value);
	}

	public Element toXmlElement() throws PropertyException {
		return null;
	}
	
}
